package com.controller;

import com.entity.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Indexed;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
    // 作业 ：1，建mvc项目
             2.编写controller类，实现1，实现跳转，2返回字符串，
             3，返回对象 。
             4.接收普通参数，
             5，接收对象，
             6，接收json的集合
*/

@Controller
public class EmployeeController {
    // 首页跳转请求 http://localhost:8080/index
    @RequestMapping("/index")
    public String t1() {
        return "index.jsp";
    }

    // 返回字符串请求 http://localhost:8080/getString
    @RequestMapping(value = "/getString", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String t2() {
        return "这是一个字符串";
    }

    // 返回对象 http://localhost:8080/getEmployee
    @RequestMapping(value = "/getEmployee")
    @ResponseBody
    public Employee t3() {
        Employee employee = new Employee(1050, "陈俊杰", "程序员", 1001, "2024-12-19", 6000.00, 0, 10);
        return employee;
    }

    // 接收普通参数 http://localhost:8080/setValue?user=陈俊杰&age=18
    @RequestMapping(value = "setValue",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String t4(String user, Integer age) {
        System.out.println(user + "今年" + age + "岁了");
        return user + "今年" + age + "岁了";
    }

    // 接收对象  http://localhost:8080/setEmployee?empno=10&ename=陈俊杰&job=程序员&mgr=1001&hiredate=2024-12-19&sal=6000.00&comm=0&deptno=10
    @RequestMapping(value = "setEmployee")
    @ResponseBody
    public Employee t5(Employee employee) {
        return employee;
    }

    // 接收json的集合  http://localhost:8080/setListJson
    /* Json 数据：
         [{
    "empno": 10,
    "ename": "陈俊杰",
    "job": "程序员",
    "mgr": 1001,
    "hiredate": "2024-12-19",
    "sal": 6000,
    "comm": 0,
    "deptno": 10
},{
    "empno": 20,
    "ename": "陈俊杰",
    "job": "程序员",
    "mgr": 1001,
    "hiredate": "2024-12-19",
    "sal": 6000,
    "comm": 0,
    "deptno": 10
},{
    "empno": 30,
    "ename": "陈俊杰",
    "job": "程序员",
    "mgr": 1001,
    "hiredate": "2024-12-19",
    "sal": 6000,
    "comm": 0,
    "deptno": 10
}]
    */
    @RequestMapping(value = "setListJson")
    @ResponseBody
    public List<Employee> t6(@RequestBody List<Employee> employeeList) {
        System.out.println(employeeList);
        return employeeList;
    }
}
